package com.ruoyi.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.system.domain.SysBuilding;
import com.ruoyi.system.domain.SysCommunity;
import com.ruoyi.system.mapper.SysBuildingMapper;
import com.ruoyi.system.mapper.SysHouseholdMapper;
import com.ruoyi.system.mq.Producer;
import com.ruoyi.system.service.ISysBuildingService;
import com.ruoyi.system.service.ISysCommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysHouseholdTaskMapper;
import com.ruoyi.system.domain.SysHouseholdTask;
import com.ruoyi.system.service.ISysHouseholdTaskService;

/**
 * 住户核酸任务Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-10-16
 */
@Service
public class SysHouseholdTaskServiceImpl implements ISysHouseholdTaskService 
{
    @Autowired
    private SysHouseholdTaskMapper sysHouseholdTaskMapper;

    @Autowired
    private ISysCommunityService sysCommunityService;

    @Autowired
    private ISysBuildingService sysBuildingService;

    @Autowired
    private Producer producer;


    /**
     * 查询住户核酸任务
     * 
     * @param id 住户核酸任务主键
     * @return 住户核酸任务
     */
    @Override
    public SysHouseholdTask selectSysHouseholdTaskById(Long id)
    {
        return sysHouseholdTaskMapper.selectSysHouseholdTaskById(id);
    }

    /**
     * 查询住户核酸任务列表
     * 
     * @param sysHouseholdTask 住户核酸任务
     * @return 住户核酸任务
     */
    @Override
    public List<SysHouseholdTask> selectSysHouseholdTaskList(SysHouseholdTask sysHouseholdTask)
    {
        return sysHouseholdTaskMapper.selectSysHouseholdTaskList(sysHouseholdTask);
    }

    /**
     * 新增住户核酸任务
     * 
     * @param sysHouseholdTask 住户核酸任务
     * @return 结果
     */
    @Override
    public int insertSysHouseholdTask(SysHouseholdTask sysHouseholdTask)
    {
        // 获取前端传递的参数
        Long communityId = sysHouseholdTask.getCommunity();
        // 查询小区名
        SysCommunity sysCommunity = sysCommunityService.selectSysCommunityById(communityId);
        String communityName = sysCommunity.getName();
        // 修改小区状态
        sysCommunity.setState(1);
        sysCommunityService.updateSysCommunity(sysCommunity);

        // 查询楼栋名
        Long buildingId = sysHouseholdTask.getBuilding();
        SysBuilding sysBuilding = sysBuildingService.selectSysBuildingById(buildingId);
        String building = sysBuilding.getBuilding();

        // 修改楼栋状态
        sysBuilding.setState(1);
        sysBuildingService.updateSysBuilding(sysBuilding);

        // 转换时间格式
        Date starttime = sysHouseholdTask.getStartTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(starttime);
        // 任务名
        sysHouseholdTask.setTask(communityName + building + format);
        // 状态
        sysHouseholdTask.setState(0);
        String str = "核酸检测结束";

        boolean b = producer.sendMessage(str);

        System.out.println("打印返回结果："+b);

        return sysHouseholdTaskMapper.insertSysHouseholdTask(sysHouseholdTask);
    }

    /**
     * 修改住户核酸任务
     * 
     * @param sysHouseholdTask 住户核酸任务
     * @return 结果
     */
    @Override
    public int updateSysHouseholdTask(SysHouseholdTask sysHouseholdTask)
    {
        return sysHouseholdTaskMapper.updateSysHouseholdTask(sysHouseholdTask);
    }

    /**
     * 批量删除住户核酸任务
     * 
     * @param ids 需要删除的住户核酸任务主键
     * @return 结果
     */
    @Override
    public int deleteSysHouseholdTaskByIds(Long[] ids)
    {
        return sysHouseholdTaskMapper.deleteSysHouseholdTaskByIds(ids);
    }

    /**
     * 删除住户核酸任务信息
     * 
     * @param id 住户核酸任务主键
     * @return 结果
     */
    @Override
    public int deleteSysHouseholdTaskById(Long id)
    {
        return sysHouseholdTaskMapper.deleteSysHouseholdTaskById(id);
    }

}
